﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Entity;
using System.Data.SqlClient;
using System.Data;

namespace Business.Dal
{
    public class UyeProvider : Provider<Uye>
    {
        public override bool Insert(Uye tObject)
        {

            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("UyeEkle");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.Parameters.Add("UlkeID", SqlDbType.Int).Value = tObject.UlkeID;
            cmd.Parameters.Add("SehirID", SqlDbType.Int).Value = tObject.SehirID;
            cmd.Parameters.Add("MahalleID", SqlDbType.Int).Value = tObject.MahalleID;
            cmd.Parameters.Add("Adi", SqlDbType.NVarChar).Value = tObject.Adi;
            cmd.Parameters.Add("Soyadi", SqlDbType.NVarChar).Value = tObject.Soyadi;
            cmd.Parameters.Add("Email", SqlDbType.NVarChar).Value = tObject.Email;
            cmd.Parameters.Add("KullaniciAdi", SqlDbType.NVarChar).Value = tObject.KullaniciAdi;
            cmd.Parameters.Add("Sifre", SqlDbType.NVarChar).Value = tObject.Sifre;
            cmd.Parameters.Add("EvAdresi", SqlDbType.NVarChar).Value = tObject.EvAdresi;
            cmd.Parameters.Add("IsAdresi", SqlDbType.NVarChar).Value = tObject.IsAdresi;
            cmd.Parameters.Add("EvTelefon", SqlDbType.VarChar).Value = tObject.EvTelefon;
            cmd.Parameters.Add("IsTelefon", SqlDbType.VarChar).Value = tObject.IsTelefon;
            cmd.Parameters.Add("GSM", SqlDbType.VarChar).Value = tObject.GSM;
            tObject.UyeID = Convert.ToInt32(cmd.ExecuteScalar());
            returnValue = true;
            return returnValue;





        }

        public override bool Update(Uye tObject)
        {
            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("UyeGuncelle");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.Parameters.Add("UlkeID", SqlDbType.Int).Value = tObject.UlkeID;
            cmd.Parameters.Add("SehirID", SqlDbType.Int).Value = tObject.SehirID;
            cmd.Parameters.Add("MahalleID", SqlDbType.Int).Value = tObject.MahalleID;
            cmd.Parameters.Add("Adi", SqlDbType.NVarChar).Value = tObject.Adi;
            cmd.Parameters.Add("Soyadi", SqlDbType.NVarChar).Value = tObject.Soyadi;
            cmd.Parameters.Add("Email", SqlDbType.NVarChar).Value = tObject.Email;
            cmd.Parameters.Add("KullaniciAdi", SqlDbType.NVarChar).Value = tObject.KullaniciAdi;
            cmd.Parameters.Add("Sifre", SqlDbType.NVarChar).Value = tObject.Sifre;
            cmd.Parameters.Add("EvAdresi", SqlDbType.NVarChar).Value = tObject.EvAdresi;
            cmd.Parameters.Add("IsAdresi", SqlDbType.NVarChar).Value = tObject.IsAdresi;
            cmd.Parameters.Add("EvTelefon", SqlDbType.VarChar).Value = tObject.EvTelefon;
            cmd.Parameters.Add("IsTelefon", SqlDbType.VarChar).Value = tObject.IsTelefon;
            cmd.Parameters.Add("GSM", SqlDbType.VarChar).Value = tObject.GSM;
            returnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());

            return returnValue;
        }

        public override bool Delete(int RecordID)
        {
            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("UyeSil");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.Parameters.Add("UlkeID", SqlDbType.Int).Value = RecordID;
            returnValue = Convert.ToBoolean(cmd.ExecuteScalar());

            return returnValue;
        }

        public List<Uye> GetAllRows()
        {
            List<Uye> returnValue = new List<Uye>();
            SqlCommand cmd = new SqlCommand("UyelerHepsi");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            dr.Read();
            while (dr.Read())
            {
                Uye U = new Uye();
                U.UyeID = dr[""] is DBNull ? 0 : Convert.ToInt32(dr[""]);
                U.SehirID = dr["SehirID"] is DBNull ? 0 : Convert.ToInt32(dr["SehirID"]);
                U.MahalleID = dr["MahalleID"] is DBNull ? 0 : Convert.ToInt32(dr["MahalleID"]);
                U.Adi = dr["Adi"] is DBNull ? String.Empty : dr["Adi"].ToString();
                U.Soyadi = dr["Soyadi"] is DBNull ? String.Empty : dr["Soyadi"].ToString();
                U.Email = dr["Email"] is DBNull ? String.Empty : dr["Email"].ToString();
                U.KullaniciAdi = dr["KullaniciAdi"] is DBNull ? String.Empty : dr["KullaniciAdi"].ToString();
                U.Sifre = dr["Sifre"] is DBNull ? String.Empty : dr["Sifre"].ToString();
                U.EvAdresi = dr["EvAdresi"] is DBNull ? String.Empty : dr["EvAdresi"].ToString();
                U.IsAdresi = dr["IsAdresi"] is DBNull ? String.Empty : dr["IsAdresi"].ToString();
                U.EvTelefon = dr["EvTelefon"] is DBNull ? String.Empty : dr["EvTelefon"].ToString();
                U.IsTelefon = dr["IsTelefon"] is DBNull ? String.Empty : dr["IsTelefon"].ToString();
                U.GSM = dr["GSM"] is DBNull ? String.Empty : dr["GSM"].ToString();
                returnValue.Add(U);
            }

            return returnValue;

        }

        public Uye GetUyeGetirID(int RecordID )
        {

            SqlCommand cmd = new SqlCommand("UyeGetirID");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            cmd.Parameters.AddWithValue("UyeID", RecordID);

            Tools.Close();
            Tools.Open();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            dr.Read();
            Uye U = new Uye();
            U.UyeID = dr["UyeID"] is DBNull ? 0 : Convert.ToInt32(dr["UyeID"]);
            U.SehirID = dr["SehirID"] is DBNull ? 0 : Convert.ToInt32(dr["SehirID"]);
            U.MahalleID = dr["MahalleID"] is DBNull ? 0 : Convert.ToInt32(dr["MahalleID"]);
            U.Adi = dr["Adi"] is DBNull ? String.Empty : dr["Adi"].ToString();
            U.Soyadi = dr["Soyadi"] is DBNull ? String.Empty : dr["Soyadi"].ToString();
            U.Email = dr["Email"] is DBNull ? String.Empty : dr["Email"].ToString();
            U.KullaniciAdi = dr["KullaniciAdi"] is DBNull ? String.Empty : dr["KullaniciAdi"].ToString();
            U.Sifre = dr["Sifre"] is DBNull ? String.Empty : dr["Sifre"].ToString();
            U.EvAdresi = dr["EvAdresi"] is DBNull ? String.Empty : dr["EvAdresi"].ToString();
            U.IsAdresi = dr["IsAdresi"] is DBNull ? String.Empty : dr["IsAdresi"].ToString();
            U.EvTelefon = dr["EvTelefon"] is DBNull ? String.Empty : dr["EvTelefon"].ToString();
            U.IsTelefon = dr["IsTelefon"] is DBNull ? String.Empty : dr["IsTelefon"].ToString();
            U.GSM = dr["GSM"] is DBNull ? String.Empty : dr["GSM"].ToString();

            dr.Dispose();
            return U;

        }

        public int Login(string KullaniciAdi, string Sifre)
        {
            SqlCommand cmd = new SqlCommand("UyeLogin");
            cmd.Parameters.Add("KullaniciAdi", SqlDbType.NVarChar).Value = KullaniciAdi;
            cmd.Parameters.Add("Sifre", SqlDbType.NVarChar).Value = Sifre;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            int UserID  = Convert.ToInt32(cmd.ExecuteScalar());

            return UserID;

          



        }

       
    }




}
